package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags = "02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @ApiOperation("发布微博")
    @PostMapping("insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        log.debug("weiboDTO"+weiboDTO);
        UserVO userVO=(UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setUserId(userVO.getId());
        weibo.setCreated(new Date());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }
    @ApiOperation("查询首页微博")
    @GetMapping("selectIndex")
    public List<WeiboIndexVO> selectIndex(){
        List<WeiboIndexVO> list = weiboMapper.selectIndex();
        return list;
    }
    @ApiOperation("查询微博详情")
    @GetMapping("selectById")
    public WeiboDetailVO selectById(Long id){

        return weiboMapper.selectById(id);
    }
}
